<?php
namespace app\controllers;


use app\common\HttpCurl;
use app\logic\MoneyLogic;
use app\logic\ShopLogic;
use app\logic\TagLogic;
use app\models\Area;
use app\common\corelib\LocalUpload;
use yii;

class ShopController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $shopLogic = new ShopLogic();
        $this->data['breadcrumbs'] = '商家管理';
        return $this->render('index', [
            'shop' => $shopLogic->getShop($query, $page),
            'query' => $query
        ]);
    }

    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['商家管理' => U('shop/index'), ' 添加商家' => ''];
            $url = Yii::$app->params['domain']['back_all_domain'];
            $latng = Yii::$app->request->get('latng');
            if($latng){
                $arr = explode(',',$latng);
                $lat = $arr[0];
                $lng = $arr[1];
            }else{
                $lat = 0;
                $lng = 0;
            }
            $tagLogic = new TagLogic();
            $choose_tag = $tagLogic->getAllTag();
            $moneyLogic = new MoneyLogic();
            $choose_commission = $moneyLogic->getPlan();
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();

            $shopLogic = new ShopLogic();
            $choose_play = $shopLogic->getAllPlay();
            return $this->render('add', [
                'actionUrl' => U('shop/add'),
                'url'=>$url,
                'choose_tag'=>$choose_tag,
                'choose_play'=>$choose_play,
                'province'=>$province,
                'theme' => '/assets/',
                'lat'=>$lat,
                'lng'=>$lng,
                'choose_commission'=>$choose_commission
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $shopLogic = new ShopLogic();
            if($_FILES['shop_pic']['name']){
                LocalUpload::go_upload($_FILES['shop_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $shopLogic->addShop($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增商家成功');
                jump('shop/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增商家失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }
    /**
     * 编辑标签
     */
    public function actionEdit()
    {
        $shopLogic = new ShopLogic();
        $tagLogic = new TagLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('shop_id');
            $params = Yii::$app->request->get('form');
            $page = Yii::$app->request->get('page', 1);
            if(!$id)
                $id = $params['shop_id'];
            $device = $shopLogic->getDeviceByShopId($id,$params,$page);
            $latng = Yii::$app->request->get('latng');
            if($latng){
                $arr = explode(',',$latng);
                $lat = $arr[0];
                $lng = $arr[1];
            }else{
                $lat = 0;
                $lng = 0;
            }
            $tag = $tagLogic->getTagByShop($id);
            $ids = array_column($tag,'tag_id');
            $tagids = implode(',',$ids);
            $choose_tag = $tagLogic->getAllTag($ids);

            $shop = $shopLogic->getById($id);

            $play = $shopLogic->getPlayByShop($id);
            $playids = array_column($play,'play_id');
            $playidss = implode(',',$playids);
            $choose_play = $shopLogic->getAllPlay($playids);

            $this->data['breadcrumbs'] = ['商家管理' => U('shop/index'), $shop['shop_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$shop['shop_pic'])){
                $shop_pic = $shop['shop_pic'];
            }else{
                $shop_pic = '/default_shop.jpg';
            }

            $shop_pics = explode(',',$shop['shop_pics']);
            foreach($shop_pics AS &$value){
                if(!file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$value)){
                    $value = '/default_shop.jpg';
                }
            }
            $moneyLogic = new MoneyLogic();
            $choose_commission = $moneyLogic->getPlan();
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();
            $city = Area::find()->where(['region_level'=>2,'parent_code'=>$shop['province_id']])->asArray()->all();
            $district = Area::find()->where(['region_level'=>3,'parent_code'=>$shop['city_id']])->asArray()->all();
            $street = Area::find()->where(['region_level'=>4,'parent_code'=>$shop['district_id']])->asArray()->all();
            $url = Yii::$app->params['domain']['back_all_domain'];
            return $this->render('edit', [
                'device'=>$device,
                'url'=>$url,
                'province'=>$province,
                'city'=>$city,
                'district'=>$district,
                'street'=>$street,
                'shop_pic'=>$shop_pic,
                'shop_pics'=>$shop_pics,
                'tagids'=>$tagids,
                'tag'=>$tag,
                'playidss'=>$playidss,
                'play'=>$play,
                'choose_tag'=>$choose_tag,
                'choose_play'=>$choose_play,
                'lat'=>$lat,
                'lng'=>$lng,
                'theme' => '/assets/',
                'shop' => $shop,
                'actionUrl' => U('shop/edit'),
                'choose_commission'=>$choose_commission
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('shop_id');
            if($_FILES['shop_pic']['name']){
                LocalUpload::go_upload($_FILES['shop_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $shopLogic->editShopById($id, $params, $uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新商家成功');
                jump('shop/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新商家失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('shop_id');
        $shopLogic = new ShopLogic();
        $logicRes = $shopLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改商家状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改商家状态失败');
        }
        return $this->redirect(U('shop/index'));
    }

    public function actionDelTag()
    {
        $shop = Yii::$app->request->post('shop_id');
        $tag = Yii::$app->request->post('tag_id');
        $shopLogic = new ShopLogic();
        $logicRes = $shopLogic->saveShopTag($shop,$tag);
        echo json_encode($logicRes);
    }

    public function actionAddDevice()
    {
        $shopLogic = new ShopLogic();
        $id = Yii::$app->request->get('shop_id');
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['商家管理' => U('shop/index'),'设备管理' => U(['shop/edit','shop_id'=>$id]), ' 添加设备' => ''];
            return $this->render('add-device', [
                'theme' => '/assets/',
                'actionUrl' => U(['shop/add-device','shop_id'=>$id])
            ]);
        }else{
            if($_FILES['device_pic']['name']){
                LocalUpload::go_upload($_FILES['device_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $params = Yii::$app->request->post('form');
            $logicRes = $shopLogic->addDevice($id,$params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增设备成功');
                jump(['shop/edit','shop_id'=>$id]);
            } else {
                Yii::$app->getSession()->setFlash('error','新增设备失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEditDevice()
    {
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('device_id');
            $device = $shopLogic->getDeviceById($id);
            $this->data['breadcrumbs'] = ['商家管理' => U('shop/index'),'设备管理' => U(['shop/edit','shop_id'=>$id]), $device['device_name'] => '', '更新' => ''];
            /*if(!$device['device_pic'] || !file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$device['device_pic'])){
                $device['device_pic'] = '/default.jpg';
            }*/
            return $this->render('edit-device', [
                'device'=>$device,
                'theme' => '/assets/',
                'actionUrl' => U('shop/edit-device'),
            ]);
        }else{
            if($_FILES['device_pic']['name']){
                LocalUpload::go_upload($_FILES['device_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('device_id');
            $logicRes = $shopLogic->editDeviceById($id, $params,$uploadresult);
            $device = $shopLogic->getDeviceById($id);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新成功');
                jump(['shop/edit','shop_id'=>$device['shop_id']]);
            } else {
                Yii::$app->getSession()->setFlash('error','更新失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionDeleteDevice()
    {
        $id = Yii::$app->request->get('device_id');
        $shopLogic = new ShopLogic();
        $device = $shopLogic->getDeviceById($id);
        $logicRes = $shopLogic->deleteDevice($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U(['shop/edit','shop_id'=>$device['shop_id']]));
    }

    public function actionTest(){
        $res = HttpCurl::get('/mafu/event/add_device',['imei'=>'123456','cat'=>0]);
        echo($res);exit;
    }

    public function actionDownloadDeviceQrcode(){
        $device_id = Yii::$app->request->get('device_id');
        $shopLogic = new ShopLogic();
        $shopLogic->downloadCode($device_id);
    }


}
